import { list, create } from '../../api/articles'

export const getArticles = () => async (dispatch) => {
  dispatch(setLoading(true))
  const { articles, articlesCount } = await list()
  dispatch(setArticles(articles))
  dispatch(setArticlesCount(articlesCount))
  dispatch(setLoading(false))
}

export const createArticle = (articles, history) => async dispatch => {
  try {
    const { article } = await create(articles)
    console.log(article)
    history.push('/')
    // console.log(res);
    // dispatch(setArticles(articles))
  } catch (error) {

  }
}

export const setArticles = articles => ({
  type: 'setArticles',
  articles
})

export const setArticlesCount = count => ({
  type: 'setArticlesCount',
  count
})

export const setLoading = loading => ({
  type: 'setArticlesLoading',
  loading
})

export const setArticle = article => ({
  type: 'setArticle',
  article
})

export const updateArticle = (key, val) => ({
  type: 'updateArticle',
  key,
  val
})